﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;

namespace FrontEndPlus.Módulos.Seguridad
{
    public class cls_Http_Remote_Download : cls_Remote_Download
    {
        public cls_Http_Remote_Download(string urlString, string descFilePath, string usuario, string password)
            : base(urlString, descFilePath, usuario, password)
        {

        }

        public override string DownloadFile()
        {
            string fileName = System.IO.Path.GetFileName(this.UrlString);
            string descFilePathAndName =
                System.IO.Path.Combine(this.DestDir, fileName);
            try
            {
                WebRequest myre = WebRequest.Create(this.UrlString);
            }
            catch(Exception ex)
            {
                return ex.Message;
            }
            try
            {
                byte[] fileData;
                using (WebClient client = new WebClient())
                {
                    fileData = client.DownloadData(this.UrlString);
                }
                using (FileStream fs =
                      new FileStream(descFilePathAndName, FileMode.OpenOrCreate))
                {
                    fs.Write(fileData, 0, fileData.Length);
                }
                return "Descarga Correcta";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}